package vn.edu.ptithcm.pim.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import vn.edu.ptithcm.pim.dom.Task;
import vn.edu.ptithcm.pim.dom.TaskPriority;

@SuppressWarnings({ "rawtypes" })
public class TaskPriorityDao extends HibernateDaoSupport {

	public void save(TaskPriority taskPriority) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().save(taskPriority);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void saveOrUpdate(TaskPriority taskPriority) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().saveOrUpdate(taskPriority);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void lock(TaskPriority taskPriority) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().lock(taskPriority, LockMode.NONE);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void delete(TaskPriority taskPriority) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().delete(taskPriority);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public TaskPriority merge(TaskPriority taskPriority) {
		try {
			return (TaskPriority) getHibernateTemplate().merge(taskPriority);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public TaskPriority findById(java.lang.Long id) {
		try {
			return (TaskPriority) getHibernateTemplate().get(
					TaskPriority.class, id);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByExample(TaskPriority taskPriority) {
		try {
			return getHibernateTemplate().findByExample("TaskPriority",
					taskPriority);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List getAll() {
		try {
			return getHibernateTemplate().loadAll(TaskPriority.class);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<TaskPriority> getPrioritiesToReport(Long projectId) {
		Criteria c = getSession().createCriteria(Task.class);
		c.setProjection(Projections.property("taskPriority"));
		c.createAlias("project", "project");
		c.add(Restrictions.eq("project.projectid", projectId));
		c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
		return c.list();
	}
}
